#include<iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include<unordered_map>
#include"log.hpp"
#include"socket.hpp"
#include"protocol.hpp"

class TcpClient{
    public:
        TcpClient(const std::string& ip,uint16_t port):_ip(ip),_socket(port){
            _socket.Connect(ip,port);
        }
        void Start(){
            std::string recv_meg,send_meg;
            while(true){
                std::cin>>send_meg;
                if(send_meg=="exit"||send_meg=="quit"||send_meg=="q"){
                    break;
                }
                else{
                    //std::cout<<send_meg<<std::endl;
                    _protocol.Serialize(send_meg);
                    if(_socket.Send(send_meg)==false){
                        break;
                    }
                    else{
                        LOG(ly::LogLevel::INFO)<<"发送成功";
                    }
                }
                _socket.Recv(recv_meg);
                _protocol.add(recv_meg);
                recv_meg=_protocol.Deserialization();
                if(recv_meg=="||"){
                    continue;
                }
                std::cout<<"收到消息: "<<recv_meg;
            }
        }
    private:
        std::string  _ip;
        int _sockfd;
        TcpSocket _socket;
        TcpProtocol _protocol;
};